#!/bin/bash
#
# Copyright (C) 2013-2021 Spring Studio
# Copyright (C) 2013-2021 Dou Dongchun <doudongchun@163.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

set -e

cd $(dirname $0)
export BUILD_ROOT=$(pwd)
export BUILD_TOOL=$(pwd)/tools
cd -

echo "========================================"
echo "[*] check env $1"
echo "========================================"
if [ ! -z "$URI" ]; then
  echo "URI=$URI (A)"
  echo "GIT=$GIT"
elif [ ! -z "$GIT" ]; then
  echo "URI=$URI"
  echo "GIT=$GIT (A)"
else
  echo "URI=$URI"
  echo "GIT=$GIT"
fi
echo "COMMIT=$COMMIT"
echo "LOCAL=$LOCAL"

# common defines
BUILD_ARCH=$1
BUILD_OPTION=$2

echo "BUILD_ARCH=$BUILD_ARCH"
echo "BUILD_OPTION=$BUILD_OPTION"
echo ""

if [ -z "$LOCAL" ]; then
  echo "You must specific the variable LOCAL"
  exit 0
fi

if [ ! -z "$URI" ]; then
  sh $BUILD_TOOL/get-repo.sh "$URI" "$LOCAL" "$COMMIT"
elif [ ! -z "$GIT" ]; then
  git --version
  sh $BUILD_TOOL/git-repo.sh "$GIT" "$LOCAL" "$COMMIT"
else
  echo "The URI and GIT were not found. This build will use local source code."
fi

init_env() {
echo "========================================"
echo "[*] config NDK $1 standalone toolchain"
echo "========================================"
. $BUILD_TOOL/do-detect-env.sh $1
}

ARCHS_32="armv7a x86"
ARCHS_64="armv7a arm64 x86 x86_64"
ARCHS_ALL=$ARCHS_64

echo_archs() {
    echo "========================================"
    echo "[*] check archs"
    echo "========================================"
    echo "ALL_ARCHS = $ARCHS_ALL"
    echo "ACT_ARCHS = $*"
    echo ""
}

echo_usage() {
    NAME=$(basename $0)
    echo "Usage:"
    echo "  $NAME armv7a|arm64|x86|x86_64"
    echo "  $NAME all|all32"
    echo "  $NAME all64"
    echo "  $NAME clean"
    exit 1
}

build_result() {
  time=$(($3 - $2))
  if (($time < 60)); then
    results=$results"\e[1;30;47mBuild $LOCAL for $1:\e[0m takes $time seconds.\n"
  else
    results=$results"\e[1;30;47mBuild $LOCAL for $1:\e[0m takes $(($time / 60)) minutes.\n"
  fi
}

#----------

results=

if [ -z "$BUILD_ARCH" ]; then
  BUILD_ARCH=arm64
fi

case "$BUILD_ARCH" in
    armv5|armv7a|arm64|x86|x86_64)
        echo_archs $BUILD_ARCH $BUILD_OPTION
        init_env $BUILD_ARCH
        start=$(date +%s)
        cd $BUILD_ROOT/$LOCAL
        build_arch $BUILD_ARCH $BUILD_OPTION
        end=$(date +%s)
        build_result $BUILD_ARCH $start $end
    ;;
    all32)
        echo_archs $ARCHS_32
        for ARCH in $ARCHS_32
        do
            init_env $ARCH
            start=$(date +%s)
            cd $BUILD_ROOT/$LOCAL
            build_arch $ARCH $BUILD_OPTION
            end=$(date +%s)
            build_result $ARCH $start $end
        done
    ;;
    all|all64)
        echo_archs $ARCHS_64
        for ARCH in $ARCHS_64
        do
            init_env $ARCH
            start=$(date +%s)
            cd $BUILD_ROOT/$LOCAL
            build_arch $ARCH $BUILD_OPTION
            end=$(date +%s)
            build_result $ARCH $start $end
        done
    ;;
    clean)
        clear_arch
    ;;
    *)
        echo_usage
        exit 1
    ;;
esac

cd -

if [ ! -z "$results" ]; then
    echo -e $results
fi

